package net.yambal.RTM.Method
{
	import flash.events.EventDispatcher;
	
	import net.yambal.RTM.Event.GroupsEvent;
	import net.yambal.RTM.Items.RTMContact;
	import net.yambal.RTM.Items.RTMGroup;
	import net.yambal.RTM.Items.RTMTransaction;
	import net.yambal.RTM.RestWrapper.RestManager;
	import net.yambal.RTM.RestWrapper.RestService;
	import net.yambal.RTM.Utilty.Parser;
	
	[Event(name="get_list", type="net.yambal.RTM.Event.GroupsEvent")]
	[Event(name="add", type="net.yambal.RTM.Event.GroupsEvent")]
	[Event(name="delete_group", type="net.yambal.RTM.Event.GroupsEvent")]
	[Event(name="add_contact", type="net.yambal.RTM.Event.GroupsEvent")]
	[Event(name="remove_contact", type="net.yambal.RTM.Event.GroupsEvent")]
	public class Groups extends EventDispatcher
	{
		public function Groups()
		{
		}
		
		// -------------------------------------------------------------------------------------
		/**
		 * getList
		 **/
		public function getList(api_key:String, shared_secret:String, auth_token:String):void{
			var p:Object = new Object();
			p.api_key = api_key;
			p.auth_token = auth_token;
			
			var RM:RestManager = new RestManager();
			RM.doMethod(RestService.METHOD_GROUPS_GET_LIST, p, this.onGetList, this.onFaultGetList, false, shared_secret);
		}
		
		private function onFaultGetList(code:Number, massage:String, ro:Object):void{
			trace(code);
			trace(massage);
		}
		
		private function onGetList(resultXML:XML, ro:Object, t:RTMTransaction):void{
			var ret:Array = Parser.GroupsXMLListToGroup(resultXML.descendants("group"));
			
			this.dispatchEvent(new GroupsEvent(GroupsEvent.GET_LIST, ret));
		}
		
		// --------------------------------------------------------------------------------
		/**
		 * add
		 **/
		public function add(api_key:String, shared_secret:String, auth_token:String, group_name:String):void{
			var p:Object = new Object();
			p.api_key = api_key;
			p.auth_token = auth_token;
			p.group = group_name;
			
			var RM:RestManager = new RestManager();
			RM.doMethod(RestService.METHOD_GROUPS_ADD, p, this.onAdd, this.onFaultAdd, true, shared_secret,"グループ追加");
		}
		
		private function onFaultAdd(code:Number, massage:String, ro:Object):void{
			// エラー処理
			trace(code);
			trace(massage);
		}
		
		private function onAdd(result_xml:XML, ro:Object, t:RTMTransaction):void{
			var ret:RTMGroup = Parser.GroupsXMLToGroup(XMLList(result_xml.descendants("group"))[0]);
			this.dispatchEvent(new GroupsEvent(GroupsEvent.ADD, null, ret, t));
		}
		
		// --------------------------------------------------------------------------------
		/**
		 * addContact
		 **/
		public function addContact(api_key:String, shared_secret:String, auth_token:String, group:RTMGroup, contact:RTMContact):void{
			var p:Object = new Object();
			p.api_key = api_key;
			p.auth_token = auth_token;
			p.group_id = group.id;
			p.contact_id = contact.id;
			
			var RM:RestManager = new RestManager();
			RM.doMethod(RestService.METHOD_GROUPS_ADD_CONTACT, p, this.onAddContact, this.onFaultAddContact, true, shared_secret,"グループにコンタクトをついか");
		}
		
		private function onFaultAddContact(code:Number, massage:String, ro:Object):void{
			// エラー処理
			trace(code);
			trace(massage);
		}
		
		private function onAddContact(result_xml:XML, ro:Object, t:RTMTransaction):void{
			this.dispatchEvent(new GroupsEvent(GroupsEvent.ADD_CONTACT, null, null, t));
		}
		
		// --------------------------------------------------------------------------------
		/**
		 * removeContact
		 **/
		public function removeContact(api_key:String, shared_secret:String, auth_token:String, group:RTMGroup, contact:RTMContact):void{
			var p:Object = new Object();
			p.api_key = api_key;
			p.auth_token = auth_token;
			p.group_id = group.id;
			p.contact_id = contact.id;
			
			var RM:RestManager = new RestManager();
			RM.doMethod(RestService.METHOD_GROUPS_REMOVE_CONTACT, p, this.onRemoveContact, this.onFaultRemoveContact, true, shared_secret,"グループにコンタクトをついか");
		}
		
		private function onFaultRemoveContact(code:Number, massage:String, ro:Object):void{
			// エラー処理
			trace(code);
			trace(massage);
		}
		
		private function onRemoveContact(result_xml:XML, ro:Object, t:RTMTransaction):void{
			this.dispatchEvent(new GroupsEvent(GroupsEvent.REMOVE_CONTACT, null, null, t));
		}
		
		// --------------------------------------------------------------------------------
		/**
		 * delete
		 **/
		public function delete_group(api_key:String, shared_secret:String, auth_token:String, group:RTMGroup):void{
			var p:Object = new Object();
			p.api_key = api_key;
			p.auth_token = auth_token;
			p.group_id = group.id;
			
			var RM:RestManager = new RestManager();
			RM.doMethod(RestService.METHOD_GROUPS_DELETE, p, this.onDelete, this.onFaultDelete, true, shared_secret, "グループ削除");
		}
		
		private function onFaultDelete(code:Number, massage:String, ro:Object):void{
			// エラー処理
			trace(code);
			trace(massage);
		}
		
		private function onDelete(result_xml:XML, ro:Object, t:RTMTransaction):void{
			trace(result_xml);
			
			this.dispatchEvent(new GroupsEvent(GroupsEvent.DELETE_GROUP));
		}
	}
}